#!/user/bin/env python
# -*- coding: utf-8 -*-
''' 
Author: Sjy
LastEditTime: 2024-11-18 10:29:39
Description: Ribbonbar 菜单类

'''


__all__= []

import os
from PyQt5 import QtCore, QtGui, QtWidgets



class Ribbonbar:
    _KEY_WIDGETS = {}
    _TITLE_CATEGORIES = {}
    _RIBBON = None
    def __init__(self, params:dict={}, *args, **kwargs) -> None:
        """初始化"""
        self.__params = params
    def set_ribbon_height(self, height:int=150) -> None:
        """设置ribbonbar的高度
        Args:
            height (int, optional): 高度. Defaults to 100.
        """
        self._RIBBON.setRibbonHeight(height)
        # self._RIBBON.update()

    def set_json_data(self, data:list=[]) -> None:
        """设置ui
        Args:
            widget (QtWidgets.QWidget, optional): ui. Defaults to None.
        """
        pass
    def add_category(self, categoryTitle:str=None) -> QtWidgets.QWidget:
        """添加一个分类
        Args:
            categoryTitle (str, optional): 分类标题. Defaults to None.

        Returns:
            QtWidgets.QWidget: 分类实例 category
        注意：
            添加的 title 可能已经存在了，因此已存在的title 则返回对应的实例
        """
        
        if categoryTitle in self._TITLE_CATEGORIES:
            return self._TITLE_CATEGORIES.get(categoryTitle).get("category")
        category = self._RIBBON.addCategory(categoryTitle)
        self._TITLE_CATEGORIES[categoryTitle] = {
            "category": category,
            "panels": {}
        }
        return category
    def remove_category(self, category:QtWidgets.QWidget=None, categoryTitle=None) -> None:
        """删除一个分类
        Args:
            category (QtWidgets.QWidget, optional): 分类实例. Defaults to None.
            categoryTitle (str, optional): 分类标题. Defaults to None.
        """
        if not category and not categoryTitle: return 
        if categoryTitle:
            category = self._TITLE_CATEGORIES.get(categoryTitle, {}).get("category")
        categoryTitle =category.title()
        self._RIBBON.removeCategory(category)
        self._TITLE_CATEGORIES.pop(categoryTitle)
    def add_panel(self, panelTitle:str=None, category:QtWidgets.QWidget=None, categoryTitle=None, showPanelOptionButton=False, titleHide=False) -> QtWidgets.QWidget:
        if not panelTitle:return
        if not category and not categoryTitle:return
        if categoryTitle:
            category = self._TITLE_CATEGORIES.get(categoryTitle, {}).get("category")
        panels = self._TITLE_CATEGORIES.get(category.title(), {}).get("panels", {})
        if panelTitle in panels:
            return panels.get(panelTitle)
        panel = category.addPanel(panelTitle, showPanelOptionButton=showPanelOptionButton)
        panels[panelTitle] = panel
        panel.layout().itemAt(1).widget().setVisible(not titleHide),

        
        return panel
    def remove_panel(self, panelTitle:str=None, category:QtWidgets.QWidget=None, categoryTitle=None) -> None:
        if not panelTitle:return
        if not category and not categoryTitle:return
        if categoryTitle:
            category = self._TITLE_CATEGORIES.get(categoryTitle, {}).get("category")
        categoryTitle = category.title()
        panels = self._TITLE_CATEGORIES.get(categoryTitle, {}).get("panels", {})
        panel = panels.get(panelTitle)
        if panel:
            panels.pop(panelTitle)
        # 注意pyqtribbon 中在添加panel时会自动添加一个分割线，因此在删除panel时也需要先删除对应的分割线
        self.remove_panel_separator(category, panelTitle)
        # 利用panel的名称移除 panel
        category.removePanel(panelTitle)
    
    def add_large_btn(self, panel:QtWidgets.QWidget=None, data:dict={}) -> None:
        if not panel:return
        if not data:return
        title = data.get("title")
        icon = data.get("icon")
        action = data.get("action")
        style = data.get("style", "large")
        styleSheet = data.get("styleSheet")
        if style == "large":
            btn = panel.addLargeButton(title)
        if icon:
            btn.setIcon(QtGui.QIcon(icon))
        if action:
            btn.clicked.connect(action)

        if styleSheet:
            btn.setStyleSheet(styleSheet)

    def remove_panel_separator(self, categoryBar, panelTitle):
        # curPanel = 
        panel = categoryBar._panels[panelTitle]
        layout = categoryBar._categoryLayout
        index = layout.indexOf(panel)
        next_widget = layout.itemAt(index + 1).widget()
        layout.removeWidget(next_widget)
    def on_load(self) -> None:
        """加载时的回调函数  菜单用不上因为没有ui，当前都是通过代码生成的或者通过数据初始化的
        """
        pass
    def on_init(self) -> None:
        """初始化时的回调函数
        """
        pass
    def on_unload(self) -> None:
        """卸载时的回调函数
        """
        pass   